/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ShippingMethodsView.java
 *
 * Created on Apr 8, 2010, 5:29:09 PM
 */
package ticketmaster.presentation.customer.purchase;

import java.awt.Color;
import java.awt.Font;
import java.util.Collection;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import ticketmaster.accounting.TaxManager;
import ticketmaster.businesslogic.OrderController;
import ticketmaster.businesslogic.ShippingMethodsController;
import ticketmaster.businessobject.PurchaseType;
import ticketmaster.businessobject.ShoppingCart;
import ticketmaster.businessobject.Ticket;
import ticketmaster.common.OrderType;
import ticketmaster.common.PurchaseTypes;
import ticketmaster.dataobject.Order;
import ticketmaster.dataobject.ShippingMethod;
import ticketmaster.presentation.customer.CustomerBasePanel;
import ticketmaster.util.DateTimeHelper;
import ticketmaster.util.StringHelper;

/**
 *
 * @author Wei Liu
 */
public class ShippingMethodsView extends CustomerBasePanel
{

    private static final Font FONT = Font.getFont("Calibri", new Font(Font.SANS_SERIF, Font.PLAIN, 18));
    private int shippingMethodID;
    private double shippingMethodPrice;
    private ShoppingCart cart;

    /** Creates new form ShippingMethodsView */
    public ShippingMethodsView()
    {
        initComponents();
        initButtonGroup();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        btnNext = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(ticketmaster.TicketMasterApp.class).getContext().getResourceMap(ShippingMethodsView.class);
        setBackground(resourceMap.getColor("Form.background")); // NOI18N
        setName("Form"); // NOI18N
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 25, 290, 30));

        btnNext.setFont(resourceMap.getFont("btnNext.font")); // NOI18N
        btnNext.setText(resourceMap.getString("btnNext.text")); // NOI18N
        btnNext.setName("btnNext"); // NOI18N
        btnNext.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNextActionPerformed(evt);
            }
        });
        add(btnNext, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 420, 150, 50));

        jLabel2.setBackground(resourceMap.getColor("jLabel2.background")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 60, 260, -1));
    }// </editor-fold>//GEN-END:initComponents

    private void btnNextActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_btnNextActionPerformed
    {//GEN-HEADEREND:event_btnNextActionPerformed
        if (this.shippingMethodID == 0)
        {
            JOptionPane.showMessageDialog(null, "Please select a shipping method first.", "Select Shipping Method", JOptionPane.INFORMATION_MESSAGE);
            return;
        }

        int eventID = 0;
        double rawCost = 0;

        Collection<Ticket> tickets = cart.getTickets();
        for (Ticket ticket : tickets)
        {
            eventID = ticket.getTicketRate().getEventID();
            rawCost += ticket.getTicketRate().getPrice();
        }

        rawCost += shippingMethodPrice;
        
        Order order = new Order();
        order.setCustomerID(this.getUser().getUserID());
        order.setOrderDate(DateTimeHelper.getCurrentDateTime());
        order.setOrderType(OrderType.TicketFee.ordinal());
        order.setShippingMethodID(shippingMethodID);
        order.setCost(rawCost + TaxManager.calculateTax(rawCost));
        order.setEventID(eventID);

        OrderController controller = new OrderController();
        order.setOrderID(controller.createOrder(order));
        if (order.getOrderID() > 0)
        {
            directToPaymentReview(rawCost, TaxManager.calculateTax(rawCost), order);
        }
    }//GEN-LAST:event_btnNextActionPerformed

    private void directToPaymentReview(double rawCost, double tax, Order order)
    {
        PaymentReviewView prv = new PaymentReviewView(rawCost, tax);
        prv.setOrder(order);
        prv.setCart(cart);
        prv.setBookedTickets(this.getBookedTickets());
        prv.setPnlParent(this.getPnlParent());
        PurchaseType.setPurchaseType(PurchaseTypes.Normal);
        this.getPnlParent().removeAll();
        this.getPnlParent().add(prv);
        this.getPnlParent().validate();
        this.getPnlParent().repaint();
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnNext;
    private javax.swing.ButtonGroup buttonGroup;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    // End of variables declaration//GEN-END:variables

    private void initButtonGroup()
    {
        ShippingMethodsController controller = new ShippingMethodsController();
        Collection<ShippingMethod> methods = controller.getAllShippingMethods();
        int y = 130;
        for (ShippingMethod method : methods)
        {
            JRadioButton button = new JRadioButton();
            button.setText(method.toString());
            button.setFont(ShippingMethodsView.FONT);
            button.setActionCommand(String.valueOf(method.getShippingMethodID()));
            button.setBackground(Color.white);
            button.addActionListener(new java.awt.event.ActionListener()
            {

                public void actionPerformed(java.awt.event.ActionEvent evt)
                {
                    shippingMethodActionPerformed(evt);
                }
            });
            add(button, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, y, 200, 40));
            buttonGroup.add(button);
            
            JLabel lblPrice = new JLabel();
            lblPrice.setText(StringHelper.formatPrice(method.getPrice()));
            lblPrice.setFont(ShippingMethodsView.FONT);
            add(lblPrice, new org.netbeans.lib.awtextra.AbsoluteConstraints(250, y, 100, 40));
            y += 60;
        }
    }

    public void shippingMethodActionPerformed(java.awt.event.ActionEvent evt)
    {
        this.shippingMethodID = Integer.parseInt(evt.getActionCommand());
        ShippingMethodsController controller = new ShippingMethodsController();
        this.shippingMethodPrice = controller.getShippingMethodPrice(shippingMethodID);
    }

    protected static ImageIcon createImageIcon(String path)
    {
        java.net.URL imgURL = ShippingMethodsView.class.getResource(path);
        return new ImageIcon(imgURL);
    }

    private static void createAndShowGUI()
    {

        //Create and set up the window.
        JFrame frame = new JFrame("demo");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Create and set up the content pane.
        ShippingMethodsView newContentPane = new ShippingMethodsView();
        newContentPane.setOpaque(true); //content panes must be opaque
        frame.setContentPane(newContentPane);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args)
    {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable()
        {

            public void run()
            {
                createAndShowGUI();
            }
        });
    }

    /**
     * @return the cart
     */
    public ShoppingCart getCart()
    {
        return cart;
    }

    /**
     * @param cart the cart to set
     */
    public void setCart(ShoppingCart cart)
    {
        this.cart = cart;
    }
}
